package expression;

import vm.runtime.NextVMRuntime;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class BoolExpression {
    public static boolean getBool(String expression, NextVMRuntime nextVMRuntime) throws Exception {

        /**
         * 1 == 1                       True
         * 1 == 2                       False
         * 1 >= 2                       False
         * True                         True
         * False                        False
         * 1 != 1                       True
         * 1 <= 10                      True
         * a() < 10                     True
         * b(10 , True , 1 < 2) == 10   False
         */

        // Remove all spaces from the expression
        expression = expression.trim();
        if (expression.equalsIgnoreCase("true")) {
            return true;
        }
        else if (expression.equalsIgnoreCase("false")) {
            return false;
        }
        else {
            String regex = "(.*?)(\\s*==\\s*|\\s*!=\\s*|\\s*>=\\s*|\\s*<=\\s*|\\s*<\\s*|\\s*>)\\s*(.*)";
            Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
            Matcher matcher = pattern.matcher(expression);
            if (matcher.matches()) {
                String part1 = matcher.group(1).trim();
                String part2 = matcher.group(2).trim();
                String part3 = matcher.group(3).trim();

                if (!StringExpression.IsNumber(part1) || !StringExpression.IsNumber(part3))
                {
                    part1 = StringExpression.getExpression(part1,nextVMRuntime);
                    part3 = StringExpression.getExpression(part3,nextVMRuntime);
                }
                //System.out.println(part1+" "+part2+" "+part3);
                return evaluateOperation(part1 , part3 , part2,nextVMRuntime);
            }else{
                throw new Exception("Bool Error: "+expression);
            }
        }
    }
    private static boolean evaluateOperation(Object left, Object right, String operator, NextVMRuntime nextVMRuntime) throws Exception {
        if (left instanceof Integer || left instanceof Long ||
            left instanceof Double || left instanceof Float
        ) {
            double leftValue = (double) left;
            double rightValue = (double) right;
            boolean result;

            switch (operator) {
                case "==" :
                    result = leftValue == rightValue;
                    break;
                case "<=" :
                    result = leftValue <= rightValue;
                    break;
                case ">=" :
                    result = leftValue >= rightValue;
                    break;
                case "!=" :
                    result = leftValue != rightValue;
                    break;
                case "<" :
                    result = leftValue < rightValue;
                    break;
                case ">" :
                    result = leftValue > rightValue;
                    break;
                default:
                    result = false;
                    break;
            }

            return result;

        } else {
            return left.equals(right);
        }
    }
}
